package com.lansam.android.utils;

import java.util.Stack;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import com.lansam.android.R;
import com.lansam.android.bean.SystemConfigInfo;

/**
 * 应用程序Activity管理类：用于Activity管理和应用程序�?�?
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-3-21
 */
public class AppManager {
    
    private static Stack<Activity> activityStack;
    private static AppManager instance;
    /** 系统配置信息 */
    private static SystemConfigInfo systemConfigInfo ;
    
    private AppManager(){}
    /**
     * 单一实例
     */
    public static AppManager getAppManager(){
        if(instance==null){
            instance=new AppManager();
        }
        return instance;
    }
    /**
     * 添加Activity到堆�?
     */
    public void addActivity(Activity activity){
        if(activityStack==null){
            activityStack=new Stack<Activity>();
        }
        activityStack.add(activity);
    }
    /**
     * 获取当前Activity（堆栈中�?���?��压入的）
     */
    public Activity currentActivity(){
        Activity activity=activityStack.lastElement();
        return activity;
    }
    /**
     * 结束当前Activity（堆栈中�?���?��压入的）
     */
    public void finishActivity(){
        Activity activity=activityStack.lastElement();
        finishActivity(activity);
    }
    /**
     * 结束指定的Activity
     */
    public void finishActivity(Activity activity){
        if(activity!=null){
            activityStack.remove(activity);
            activity.finish();
            activity=null;
        }
    }
    /**
     * 结束指定类名的Activity
     */
    public void finishActivity(Class<?> cls){
        for (Activity activity : activityStack) {
            if(activity.getClass().equals(cls) ){
                finishActivity(activity);
            }
        }
    }
    /**
     * 结束�?��Activity
     */
    public void finishAllActivity(){
        for (int i = 0, size = activityStack.size(); i < size; i++){
            if (null != activityStack.get(i)){
                activityStack.get(i).finish();
            }
        }
        activityStack.clear();
    }
    /**
     * �?��应用程序
     */
    public void AppExit(Context context) {
        try {
            finishAllActivity();
            ActivityManager activityMgr= (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            activityMgr.restartPackage(context.getPackageName());
            System.exit(0);
        } catch (Exception e) { }
    }
    
    /**
     * @return the activityStack
     */
    public static Stack<Activity> getActivityStack() {
        return activityStack;
    }
    /**
     * @param activityStack the activityStack to set
     */
    public static void setActivityStack(Stack<Activity> activityStack) {
        AppManager.activityStack = activityStack;
    }
    /**
     * @return the instance
     */
    public static AppManager getInstance() {
        return instance;
    }
    /**
     * @param instance the instance to set
     */
    public static void setInstance(AppManager instance) {
        AppManager.instance = instance;
    }
    /**
     * @return the 系统配置信息
     */
    public static SystemConfigInfo getSystemConfigInfo() {
        if(systemConfigInfo == null){
            systemConfigInfo = new SystemConfigInfo();
            
        }
        
        return systemConfigInfo;
    }
    /**
     * @param 系统配置信息 the systemConfigInfo to set
     */
    public static void setSystemConfigInfo(SystemConfigInfo systemConfigInfo) {
        AppManager.systemConfigInfo = systemConfigInfo;
    }
    
    /**
     * 退出程序
     * 
     * @param cont
     */
    public static void Exit(final Context cont) {
        AlertDialog.Builder builder = new AlertDialog.Builder(cont);
        builder.setIcon(android.R.drawable.ic_dialog_info);
        builder.setTitle(R.string.app_menu_surelogout);
        builder.setPositiveButton(R.string.sure,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        // 退出
                        AppManager.getAppManager().AppExit(cont);
                    }
                });
        builder.setNegativeButton(R.string.cancle,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
        builder.show();
    }
    
}